package com.cxxx.guns.modular.backend.controller;

import com.cxxx.guns.core.base.controller.BaseController;
import com.cxxx.guns.core.support.BeanKit;
import com.cxxx.guns.modular.backend.warpper.ImageWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.cxxx.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.cxxx.guns.modular.backend.model.Image;
import com.cxxx.guns.modular.backend.service.IImageService;

import java.util.List;

/**
 * 图片管理控制器
 *
 * @author fengshuonan
 * @Date 2018-09-15 10:50:45
 */
@Controller
@RequestMapping("/image")
public class ImageController extends BaseController {

    private String PREFIX = "/backend/image/";

    @Autowired
    private IImageService imageService;

    /**
     * 跳转到图片管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "image.html";
    }

    /**
     * 跳转到添加图片管理
     */
    @RequestMapping("/image_add")
    public String imageAdd() {
        return PREFIX + "image_add.html";
    }

    /**
     * 跳转到修改图片管理
     */
    @RequestMapping("/image_update/{imageId}")
    public String imageUpdate(@PathVariable Integer imageId, Model model) {
        Image image = imageService.selectById(imageId);
        model.addAttribute("item",image);
        LogObjectHolder.me().set(image);
        return PREFIX + "image_edit.html";
    }

    /**
     * 获取图片管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List list =  imageService.selectList(null);
        return new ImageWarpper(BeanKit.listToMapList(list)).warp();
    }

    /**
     * 新增图片管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Image image) {
        imageService.insert(image);
        return SUCCESS_TIP;
    }

    /**
     * 删除图片管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer imageId) {
        imageService.deleteById(imageId);
        return SUCCESS_TIP;
    }

    /**
     * 修改图片管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Image image) {
        imageService.updateById(image);
        return SUCCESS_TIP;
    }

    /**
     * 图片管理详情
     */
    @RequestMapping(value = "/detail/{imageId}")
    @ResponseBody
    public Object detail(@PathVariable("imageId") Integer imageId) {
        return imageService.selectById(imageId);
    }
}
